package com.gqj.meter.service.impl;

import com.gqj.common.constant.BusinessConstants;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.StorageUtil;
import com.gqj.common.utils.StringUtils;
import com.gqj.meter.domain.*;
import com.gqj.meter.mapper.*;
import com.gqj.meter.service.IMdOutTaskDetailsService;
import com.gqj.meter.service.IMdOutTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 出入库任务Service业务层处理
 *
 * @author lxq
 * @date lxq-02-10
 */
@Service
public class MdOutTaskServiceImpl implements IMdOutTaskService {
    @Autowired
    private MdOutTaskMapper mdOutTaskMapper;
    @Autowired
    private MdOutTaskDetailsMapper mdOutTaskDetailsMapper;
    @Autowired
    private IMdOutTaskDetailsService mdOutTaskDetailsService;
    @Autowired
    private MdMeterMapper mdMeterMapper;
    @Autowired
    private MdItMapper mdItMapper;
    @Autowired
    private MdConcentratorMapper mdConcentratorMapper;

    /**
     * 查询出入库任务
     *
     * @param outTaskId 出入库任务主键
     * @return 出入库任务
     */
    @Override
    public MdOutTask selectMdOutTaskByOutTaskId(Long outTaskId) {
        return mdOutTaskMapper.selectMdOutTaskByOutTaskId(outTaskId);
    }

    /**
     * 查询出入库任务列表
     *
     * @param mdOutTask 出入库任务
     * @return 出入库任务
     */
    @Override
    public List<MdOutTask> selectMdOutTaskList(MdOutTask mdOutTask) {
        return mdOutTaskMapper.selectMdOutTaskList(mdOutTask);
    }

    /**
     * 新增出入库任务
     *
     * @param mdOutTask 出入库任务
     * @return 结果
     */
    @Override
    public int insertMdOutTask(MdOutTask mdOutTask) {
        //任务单号
        mdOutTask.setOutTaskNo("Ck-"+ StorageUtil.getOrderNoTo12());
        return mdOutTaskMapper.insertMdOutTask(mdOutTask);
    }

    /**
     * 修改出入库任务
     *
     * @param mdOutTask 出入库任务
     * @return 结果
     */
    @Override
    public int updateMdOutTask(MdOutTask mdOutTask) {
        return mdOutTaskMapper.updateMdOutTask(mdOutTask);
    }

    /**
     * 修改出入库任务状态
     *
     * @param mdOutTask 出入库任务
     * @return 结果
     */
    @Override
    public int updateMdOutTaskStatus(MdOutTask mdOutTask) {
        MdOutTaskDetails MdOutTaskDetailsQuery = new MdOutTaskDetails();
        MdOutTaskDetailsQuery.setOutTaskNo(mdOutTask.getOutTaskNo());
        List<MdOutTaskDetails> MdOutTaskDetailsList = mdOutTaskDetailsMapper.selectMdOutTaskDetailsList(MdOutTaskDetailsQuery);

        for (MdOutTaskDetails mdOutTaskDetails : MdOutTaskDetailsList) {
            if ("0".equals(mdOutTaskDetails.getIsComplete())) {
                mdOutTaskDetailsService.updateServiceStatus(mdOutTask.getAssetsCategory(), mdOutTaskDetails.getAssetsId(), "01");
            }
        }
        return mdOutTaskMapper.updateMdOutTask(mdOutTask);
    }

    /**
     * 批量删除出入库任务
     *
     * @param outTaskIds 需要删除的出入库任务主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMdOutTaskByOutTaskIds(Long[] outTaskIds) {
        for (Long outTaskId : outTaskIds) {
            MdOutTask mdOutTask = mdOutTaskMapper.selectMdOutTaskByOutTaskId(outTaskId);
            if ("1".equals(mdOutTask.getStatusCode())) {
                throw new ServiceException("删除'" + mdOutTask.getOutTaskNo() + "'任务失败，任务已执行不允许删除");
            }
            MdOutTaskDetails mdOutTaskDetailsQuery = new MdOutTaskDetails();
            mdOutTaskDetailsQuery.setOutTaskNo(mdOutTask.getOutTaskNo());
            List<MdOutTaskDetails> mdOutTaskDetailsList = mdOutTaskDetailsMapper.selectMdOutTaskDetailsList(mdOutTaskDetailsQuery);

            for (MdOutTaskDetails mdOutTaskDetails : mdOutTaskDetailsList) {
                //返回状态
                mdOutTaskDetailsService.updateServiceStatus(mdOutTask.getAssetsCategory(), mdOutTaskDetails.getAssetsId(), "01");
            }

            mdOutTaskDetailsMapper.deleteMdOutTaskDetailsByOutTaskNo(mdOutTask.getOutTaskNo());
        }
        return mdOutTaskMapper.deleteMdOutTaskByOutTaskIds(outTaskIds);
    }

    /**
     * 删除出入库任务信息
     *
     * @param outTaskId 出入库任务主键
     * @return 结果
     */
    @Override
    public int deleteMdOutTaskByOutTaskId(Long outTaskId) {
        return mdOutTaskMapper.deleteMdOutTaskByOutTaskId(outTaskId);
    }

    /**
     * 校验任务编号是否唯一
     *
     * @param mdOutTask 任务信息
     * @return 结果
     */
    @Override
    public String checkInTaskNoUnique(MdOutTask mdOutTask) {
        Long outTaskId = StringUtils.isNull(mdOutTask.getOutTaskId()) ? -1L : mdOutTask.getOutTaskId();
        MdOutTask info = mdOutTaskMapper.checkOutTaskNoUnique(mdOutTask.getOutTaskNo());
        if (StringUtils.isNotNull(info) && info.getOutTaskId().longValue() != outTaskId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增出出库任务(工作台)
     *
     * @param mdOutTask 出库任务
     * @return 结果
     */
    @Override
    public int insertMdOutTaskWorktop(MdOutTask mdOutTask) {
        //任务单号
        mdOutTask.setOutTaskNo("Ck"+ StorageUtil.getOrderNoTo12());
        //任务状态默认完成
        mdOutTask.setStatusCode(BusinessConstants.STATUS_CODE_END);
        //操作时间
        mdOutTask.setOperationDate(DateUtils.getNowDate());
        //创建时间
        mdOutTask.setCreateTime(DateUtils.getNowDate());
        //任务时间
        mdOutTask.setTaskDate(DateUtils.getNowDate());
        int i = mdOutTaskMapper.insertMdOutTask(mdOutTask);
        if (i > 0) {
            for (MdOutTaskDetails mdOutTaskDetails : mdOutTask.getMdOutTaskDetailsList()) {
                mdOutTaskDetailsService.updateServiceStatus(mdOutTask.getAssetsCategory(), mdOutTaskDetails.getAssetsId(), "02");
            }


            MdOutTaskDetails mdOutTaskDetailsQueryCount = new MdOutTaskDetails();
            mdOutTaskDetailsQueryCount.setOutTaskNo(mdOutTask.getOutTaskNo());

            for (MdOutTaskDetails mdOutTaskDetails : mdOutTask.getMdOutTaskDetailsList()) {
                //使用设备
                mdOutTaskDetails.setUseEquipment(BusinessConstants.USE_EQUIPMENT_WORKTOP);
                mdOutTaskDetails.setOutTaskNo(mdOutTask.getOutTaskNo());
                MdOutTaskDetails mdOutTaskDetailsQuery = new MdOutTaskDetails();
                //已完成
                mdOutTaskDetails.setIsComplete(BusinessConstants.IS_COMPLETE_END);
                mdOutTaskDetailsQuery.setRfidNo(mdOutTaskDetails.getRfidNo());
//                MdOutTaskDetails mdOutTaskDetailsTemp = mdOutTaskDetailsMapper.selectCarryMdOutTaskDetailsCount(mdOutTaskDetailsQuery);
//                if (StringUtils.isNotNull(mdOutTaskDetailsTemp)) {
//                    if (mdOutTaskDetailsTemp.getRepeatNumber() > 0) {
//                        throw new ServiceException("条形码 " + mdOutTaskDetails.getRfidNo() + " 的信息在" + mdOutTaskDetailsTemp.getOutTaskNo() + "的任务中已存在");
//                    }
//                }
                //查询物资详情插入详情表
                if (mdOutTask.getAssetsCategory().equals(BusinessConstants.ASSETS_CATEGORY_METER)) {
                    //电能表
                    MdMeter meter = mdMeterMapper.selectMdMeterByRfidNo(mdOutTaskDetails.getRfidNo());
                    if (meter == null) {
                        throw new ServiceException("电能表信息不存在，请检查后重试！");
                    }
                    mdOutTaskDetails.setAssetsId(meter.getMeterId());
                    mdOutTaskDetails.setRfidNo(meter.getRfidNo());
                    mdOutTaskDetails.setMadeNo(meter.getMadeNo());
                    mdOutTaskDetails.setSortCode(meter.getSortCode());
                    mdOutTaskDetails.setTypeCode(meter.getTypeCode());
                    mdOutTaskDetails.setEquipCode(meter.getEquipCode());
                    mdOutTaskDetails.setEquipName(meter.getEquipName());

                    //修改电能表状态
                    mdMeterMapper.updateMdMeter(new MdMeter(){{
                        setMeterId(meter.getMeterId());
                        setServiceStatus(BusinessConstants.SERVICE_STATUS_OUT);
                    }});
                } else if (mdOutTask.getAssetsCategory().equals(BusinessConstants.ASSETS_CATEGORY_IT)) {
                    //互感器
                    MdIt it = mdItMapper.selectMdItByRfidNo(mdOutTaskDetails.getRfidNo());
                    if (it == null) {
                        throw new ServiceException("互感器信息不存在请检查后重试");
                    }
                    mdOutTaskDetails.setAssetsId(it.getItId());
                    mdOutTaskDetails.setRfidNo(it.getRfidNo());
                    mdOutTaskDetails.setMadeNo(it.getMadeNo());
                    mdOutTaskDetails.setSortCode(it.getSortCode());
                    mdOutTaskDetails.setTypeCode(it.getTypeCode());
                    mdOutTaskDetails.setEquipCode(it.getEquipCode());
                    mdOutTaskDetails.setEquipName(it.getEquipName());

                    //修改互感器状态
                    mdItMapper.updateMdIt(new MdIt(){{
                        setItId(it.getItId());
                        setServiceStatus(BusinessConstants.SERVICE_STATUS_OUT);
                    }});
                } else if (mdOutTask.getAssetsCategory().equals(BusinessConstants.ASSETS_CATEGORY_CONCENTRATOR)) {
                    //采集器
                    MdConcentrator concentrator = mdConcentratorMapper.selectMdConcentratorByRfidNo(mdOutTaskDetails.getRfidNo());
                    if (concentrator == null) {
                        throw new ServiceException("采集器信息不存在请检查后重试");
                    }
                    mdOutTaskDetails.setAssetsId(concentrator.getConcentratorId());
                    mdOutTaskDetails.setRfidNo(concentrator.getRfidNo());
                    mdOutTaskDetails.setMadeNo(concentrator.getMadeNo());
                    mdOutTaskDetails.setTypeCode(concentrator.getTypeCode());
                    mdOutTaskDetails.setEquipCode(concentrator.getEquipCode());
                    mdOutTaskDetails.setEquipName(concentrator.getEquipName());

                    //修改采集器状态
                    mdConcentratorMapper.updateMdConcentrator(new MdConcentrator(){{
                        setConcentratorId(concentrator.getConcentratorId());
                        setServiceStatus(BusinessConstants.SERVICE_STATUS_OUT);
                    }});
                }

                mdOutTaskDetailsMapper.insertMdOutTaskDetails(mdOutTaskDetails);
            }
        }
        return i;
    }
}
